package com.rjt.file.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import com.rjt.common.util.CuccUtil;
import com.rjt.file.service.FileService;

/**  
* @comment
* @author renjiangtao 
* @date 2019年2月28日  
* @version 1.0  
*/
@Service
public class FileServiceImpl implements FileService{
	@Autowired
	private JavaMailSender mailSender;
	@Override
	public void save(final String mobile, final String num,final String rate,final String mail,final String encryptmobile)  {
		// TODO Auto-generated method stub
		final List<String> paths=new ArrayList<String>();
		paths.add("C:\\Users\\Administrator\\Downloads\\apache-tomcat-8.0.52-windows-x64\\apache-tomcat-8.0.52\\webapps\\ROOT\\tanranWeb.txt");
		paths.add("C:\\Users\\Administrator\\Downloads\\apache-tomcat-8.0.52-windows-x64\\apache-tomcat-8.0.52\\webapps\\ROOT\\tanranPc.txt");
		int initNum=CuccUtil.getNum(mobile,encryptmobile);
		CuccUtil.writeData(mobile,rate,paths);
		Runnable task=new Task(mobile,paths,mail,initNum,num,mailSender);
		new Thread(task).start();
	}
	
	


}
class Task implements Runnable {
	
	
	private String mobile;
	private List<String> paths;
	private String mail;
	private int initNum;
	private String num;
	private String encryptmobile;
	
	private JavaMailSender mailSender;
	
	
	public Task(String mobile, List<String> paths, String mail, int initNum, String num,JavaMailSender mailSender) {
		super();
		this.mobile = mobile;
		this.paths = paths;
		this.mail = mail;
		this.initNum = initNum;
		this.num = num;
		this.mailSender=mailSender;
	}



	@Override
	public void run() {
		final Timer timer = new Timer();
		TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
            	if(CuccUtil.isComplete(mobile, initNum, num,encryptmobile)) {
            		timer.cancel();
                	System.out.println("over");
                	CuccUtil.writeData(mobile,"9",paths);
                	CuccUtil.sendSimpleMail(mobile+"已经充值完毕",mail,mailSender);
            	}
            }
        };
        timer.schedule(timerTask,10,1000*60);
	}
	

	
}